package com.tom.compose.service.logic;

import com.tom.compose.model.vo.TreeNodeLink;

import java.util.List;
import java.util.Map;

/**
 * @author zx
 * @date 2024-10-09 19:52
 */
public abstract class BaseLogic implements LogicFilter{

    @Override
    public abstract String matterValue(Long treeId, String userId, Map<String, String> decisionMatter);

    /**
     * 逻辑决策器
     *
     * @param matterValue          决策值
     * @param treeNodeLinkList 决策节点
     * @return 下一个节点Id
     */
    @Override
    public Long filter(String matterValue, List<TreeNodeLink> treeNodeLinkList) {
        for (TreeNodeLink nodeLine : treeNodeLinkList) {
            if (decisionLogic(matterValue, nodeLine)) {
                return nodeLine.getNodeIdTo();
            }
        }
        return 0L;
    }

    /**
     * 几本决策方法
     * @param matterValue 决策值
     * @param nodeLink 规则树线信息:树节点链路关系
     * @return true通过,false不通过
     */
    private boolean decisionLogic(String matterValue, TreeNodeLink nodeLink) {
        switch (nodeLink.getRuleLimitType()) {
            case 1:
                // 等于
                return matterValue.equals(nodeLink.getRuleLimitValue());
            case 2:
                // 大于
                return Double.parseDouble(matterValue) > Double.parseDouble(nodeLink.getRuleLimitValue());
            case 3:
                // 小于
                return Double.parseDouble(matterValue) < Double.parseDouble(nodeLink.getRuleLimitValue());
            case 4:
                // 大于等于
                return Double.parseDouble(matterValue) <= Double.parseDouble(nodeLink.getRuleLimitValue());
            case 5:
                // 小于等于
                return Double.parseDouble(matterValue) >= Double.parseDouble(nodeLink.getRuleLimitValue());
            default:
                return false;
        }
    }

}
